using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows.Controls;
using Buddy.Coroutines;
using log4net;
using Triton.Common;
using Triton.Common.LogUtilities;
using Triton.Game;
using Triton.Game.Mapping;

namespace Triton.Bot.Logic.Bots.NewDefaultBot
{
	public class NewDefaultBot
	{
		[Serializable]
		[CompilerGenerated]
		private sealed class Class222
		{
			public static readonly Class222 Instance9 = new Class222();

			public static Func<CollectibleCard, bool> Instance9__24_0;

			internal bool method_0(CollectibleCard collectibleCard_0)
			{
				if (collectibleCard_0.m_EntityDef.GetClass() != TAG_CLASS.NEUTRAL)
				{
					if (collectibleCard_0.m_EntityDef.GetClass() == TAG_CLASS.ROGUE)
					{
						return !collectibleCard_0.m_EntityDef.IsHero();
					}
					return false;
				}
				return true;
			}
		}

		private static readonly ILog ilog_0 = Triton.Common.LogUtilities.Logger.GetLoggerInstanceForType();

		private Coroutine coroutine_0;

		private readonly TaskManager taskManager_0 = new TaskManager();

		private NewDefaultBotGui newDefaultBotGui_0;

		private static Random random_0 = new Random();

		public string Name => "NewDefaultBot";

		public string Description => "The new default bot for Hearthbuddy.";

		public string Author => "Bossland GmbH";

		public string Version => "0.0.1.1";

		public JsonSettings Settings => NewDefaultBotSettings.Instance;

		public UserControl Control
		{
			get
			{
				if (newDefaultBotGui_0 == null)
				{
					newDefaultBotGui_0 = new NewDefaultBotGui();
				}
				return newDefaultBotGui_0;
			}
		}

		private void method_0()
		{
			if (coroutine_0 != null)
			{
				coroutine_0.Dispose();
			}
			coroutine_0 = new Coroutine(() => method_1());
		}

		public void Initialize()
		{
		}

		public void Deinitialize()
		{
		}

		public void Start()
		{
			method_0();
			PluginManager.Start();
			RoutineManager.Start();
			ProcessHookManager.Enable();
			AchieveManager achieveManager = AchieveManager.Get();
			for (int i = 0; i < 1024; i++)
			{
				Achievement achievement = achieveManager.GetAchievement(i);
				if (achievement != null)
				{
					ilog_0.InfoFormat("[{0}] {1}: {2}", achievement.ID, achievement.Name, achievement.Description);
				}
			}
			BotManager.Stop();
		}

		public void Tick()
		{
			if (coroutine_0.IsFinished)
			{
				ilog_0.DebugFormat("The bot coroutine has finished in a state of {0}", coroutine_0.Status);
				BotManager.Stop();
				return;
			}
			if (!TritonHs.IsClientInUsableState(logReason: true))
			{
				BotManager.MsBeforeNextTick += 750;
				method_0();
				return;
			}
			PluginManager.Tick();
			RoutineManager.Tick();
			try
			{
				coroutine_0.Resume();
			}
			catch
			{
				Coroutine coroutine = coroutine_0;
				coroutine_0 = null;
				coroutine.Dispose();
				throw;
			}
		}

		public void Stop()
		{
			if (coroutine_0 != null)
			{
				coroutine_0.Dispose();
				coroutine_0 = null;
			}
			PluginManager.Stop();
			RoutineManager.Stop();
			ProcessHookManager.Disable();
		}

		public override string ToString()
		{
			return Name + ": " + Description;
		}

		private async Task method_1()
		{
			bool flag = true;
			while (true)
			{
				if (flag)
				{
					return;
				}
				await Coroutine.Yield();
				CollectionManagerDisplay collectionManagerDisplay = CollectionManagerDisplay.Get();
				if (collectionManagerDisplay == null || !collectionManagerDisplay.IsReady())
				{
					continue;
				}
				CollectionDeckTray collectionDeckTray = CollectionDeckTray.Get();
				if (collectionDeckTray == null)
				{
					ilog_0.WarnFormat("collectionDeckTray == null");
					await Coroutine.Sleep(1000);
					continue;
				}
				CollectionManager collectionManager = CollectionManager.Get();
				if (collectionManager != null && collectionManager.IsFullyLoaded())
				{
					if (collectionManagerDisplay.m_selectingNewDeckHero)
					{
						ilog_0.Info("m_selectingNewDeckHero");
						HeroPickerDisplay heroPickerDisplay = collectionManagerDisplay.m_heroPickerDisplay;
						if (heroPickerDisplay == null)
						{
							ilog_0.WarnFormat("heroPickerDisplay == null");
							await Coroutine.Sleep(1000);
							continue;
						}
						if (!heroPickerDisplay.IsShown())
						{
							ilog_0.WarnFormat("!heroPickerDisplay.IsShown()");
							await Coroutine.Sleep(1000);
							continue;
						}
						DeckPickerTrayDisplay deckPickerTray = heroPickerDisplay.m_deckPickerTray;
						if (deckPickerTray == null)
						{
							ilog_0.WarnFormat("deckPickerTray == null");
							await Coroutine.Sleep(1000);
							continue;
						}
						if (!deckPickerTray.IsChoosingHero())
						{
							ilog_0.WarnFormat("!deckPickerTray.IsChoosingHero()");
							await Coroutine.Sleep(1000);
							continue;
						}
						bool flag2 = true;
						if (deckPickerTray.m_selectedHeroButton != null && deckPickerTray.m_selectedHeroButton.m_heroClass == TAG_CLASS.ROGUE)
						{
							flag2 = false;
						}
						if (flag2)
						{
							ilog_0.Info("Now choosing the Rogue hero...");
							HeroPickerButton preconButtonForClass = deckPickerTray.GetPreconButtonForClass(TAG_CLASS.ROGUE);
							if (preconButtonForClass == null || !preconButtonForClass.IsEnabled() || preconButtonForClass.IsLocked())
							{
								ilog_0.WarnFormat("button == null || !button.IsEnabled() || button.IsLocked()");
								await Coroutine.Sleep(1000);
								return;
							}
							Client.LeftClickAt(preconButtonForClass.Transform.Position);
							await Coroutine.Sleep(3000);
						}
						else
						{
							PlayButton playButton = deckPickerTray.m_playButton;
							if (playButton == null || !playButton.m_enabled)
							{
								break;
							}
							ilog_0.Info("Now confirming hero...");
							Client.LeftClickAt(playButton.Transform.Position);
							await Coroutine.Sleep(5000);
						}
					}
					else if (collectionManager.IsInEditMode())
					{
						CollectionPageManager pageManager = collectionManagerDisplay.m_pageManager;
						if (pageManager == null)
						{
							ilog_0.WarnFormat("pageManager.ArePagesTurning()");
							await Coroutine.Sleep(1000);
							continue;
						}
						if (pageManager.ArePagesTurning())
						{
							ilog_0.WarnFormat("pageManager.ArePagesTurning()");
							await Coroutine.Sleep(1000);
							continue;
						}
						ilog_0.Info("IsInEditMode");
						DeckTemplatePicker deckTemplatePicker = pageManager.m_deckTemplatePicker;
						if (deckTemplatePicker != null && deckTemplatePicker.IsShowingPacks())
						{
							bool flag3 = true;
							if (deckTemplatePicker.m_currentSelectedDeck != null)
							{
								ilog_0.InfoFormat("m_class: {0}", deckTemplatePicker.m_currentSelectedDeck.m_class);
								ilog_0.InfoFormat("m_id: {0}", deckTemplatePicker.m_currentSelectedDeck.m_id);
								ilog_0.InfoFormat("m_title: {0}", deckTemplatePicker.m_currentSelectedDeck.m_title);
								ilog_0.InfoFormat("m_description: {0}", deckTemplatePicker.m_currentSelectedDeck.m_description);
								ilog_0.InfoFormat("m_sortOrder: {0}", deckTemplatePicker.m_currentSelectedDeck.m_sortOrder);
								if (deckTemplatePicker.m_currentSelectedDeck.m_class == TAG_CLASS.INVALID)
								{
									flag3 = false;
								}
							}
							if (flag3)
							{
								ilog_0.Info("Now clicking on the Custom Deck template...");
								Client.LeftClickAt(deckTemplatePicker.m_customDeckButton.Transform.Position);
								await Coroutine.Sleep(5000);
							}
							else
							{
								ilog_0.Info("Now confirming the Custom Deck template...");
								Client.LeftClickAt(deckTemplatePicker.m_chooseButton.Transform.Position);
								await Coroutine.Sleep(5000);
							}
						}
						else
						{
							List<CollectibleCard> list = (from collectibleCard_0 in collectionManager.GetOwnedCards()
								where collectibleCard_0.m_EntityDef.GetClass() == TAG_CLASS.NEUTRAL || (collectibleCard_0.m_EntityDef.GetClass() == TAG_CLASS.ROGUE && !collectibleCard_0.m_EntityDef.IsHero())
								select collectibleCard_0).ToList();
							CollectibleCard collectibleCard = list[random_0.Next(0, list.Count())];
							ilog_0.InfoFormat("JumpToPageWithCard => {0} {1}", collectibleCard.CardId, collectibleCard.PremiumType);
							bool flag4 = pageManager.JumpToPageWithCard(collectibleCard.CardId, collectibleCard.PremiumType);
							ilog_0.InfoFormat("JumpToPageWithCard: {0}", flag4, collectibleCard.CardId, collectibleCard.PremiumType);
							await Coroutine.Sleep(5000);
						}
					}
					else
					{
						UIBScrollable scrollbar = collectionDeckTray.m_scrollbar;
						if (scrollbar != null && scrollbar.IsEnabled())
						{
							collectionDeckTray.m_scrollbar.SetScrollSnap(100f, clamp: true);
							await Coroutine.Sleep(3000);
						}
						CollectionNewDeckButton newDeckButton = collectionDeckTray.GetDecksContent().m_newDeckButton;
						if (newDeckButton == null)
						{
							ilog_0.WarnFormat("newDeckButton == null");
							await Coroutine.Sleep(1000);
						}
						else if (!newDeckButton.GameObject.Active)
						{
							ilog_0.WarnFormat("!newDeckButton.GameObject.Active");
							await Coroutine.Sleep(1000);
						}
						else if (!newDeckButton.IsUsable())
						{
							ilog_0.WarnFormat("!newDeckButton.IsUsable()");
							await Coroutine.Sleep(1000);
						}
						else if (!newDeckButton.IsEnabled())
						{
							ilog_0.WarnFormat("!newDeckButton.IsEnabled()");
						}
						else
						{
							ilog_0.InfoFormat("Now clicking on the new deck button...");
							Client.LeftClickAt(collectionDeckTray.GetDecksContent().GetNewDeckButtonPosition());
							await Coroutine.Sleep(5000);
						}
					}
				}
				else
				{
					ilog_0.WarnFormat("collectionManager == null || !collectionManager.IsFullyLoaded()");
					await Coroutine.Sleep(1000);
				}
			}
			ilog_0.WarnFormat("playButton == null || !playButton.m_enabled");
			await Coroutine.Sleep(1000);
		}

		private async Task method_2()
		{
			bool flag = false;
			bool flag2 = false;
			while (!flag)
			{
				CollectionManagerDisplay collectionManagerDisplay = CollectionManagerDisplay.Get();
				if (collectionManagerDisplay == null)
				{
					await Coroutine.Sleep(3000);
				}
				else if (!collectionManagerDisplay.IsReady())
				{
					await Coroutine.Sleep(3000);
				}
				else
				{
					if (CollectionManager.Get().GetTaggedDeck(CollectionManager.DeckTag.Editing).GetTotalCardCount() >= DeckRuleset.GetStandardRuleset().GetDeckSize())
					{
						flag = true;
					}
					CollectionPageManager pageManager = collectionManagerDisplay.m_pageManager;
					if (pageManager != null)
					{
						if (pageManager.ArePagesTurning())
						{
							await Coroutine.Sleep(3000);
						}
						else
						{
							if (flag2)
							{
								ilog_0.InfoFormat("[MainCoroutine] Now flipping the page.");
								if (pageManager.m_pageRightClickableRegion != null)
								{
									if (!pageManager.m_pageRightClickableRegion.IsEnabled())
									{
										ilog_0.InfoFormat("[MainCoroutine] m_pageRightClickableRegion.IsEnabled");
									}
								}
								else
								{
									ilog_0.InfoFormat("[MainCoroutine] m_pageRightClickableRegion == null");
								}
								await Coroutine.Sleep(3000);
								continue;
							}
							ilog_0.InfoFormat("[MainCoroutine] m_currentPageNum: {0}", pageManager.m_currentPageNum);
							if (pageManager.m_currentPageNum < pageManager.GetLastPageInCurrentMode())
							{
								flag2 = true;
							}
							CollectionPageDisplay[] array = new CollectionPageDisplay[2] { pageManager.m_pageA, pageManager.m_pageB };
							foreach (CollectionPageDisplay collectionPageDisplay in array)
							{
								if (flag)
								{
									break;
								}
								if (collectionPageDisplay == null)
								{
									continue;
								}
								bool flag3 = false;
								int num = 0;
								while (!flag)
								{
									List<CollectionCardVisual> collectionCardVisuals = collectionPageDisplay.m_collectionCardVisuals;
									if (collectionCardVisuals != null)
									{
										foreach (CollectionCardVisual item in collectionCardVisuals)
										{
											if (flag)
											{
												break;
											}
											if (item.m_shown)
											{
												int count = item.m_count.GetCount();
												bool flag4 = item.CanPickUpCard();
												string cardId = item.GetActor().m_entityDef.m_cardId;
												ilog_0.InfoFormat("[MainCoroutine] {0}x {1}{2}", count, cardId, flag4 ? "" : " [CANNOT USE]");
												if (flag4)
												{
													item.OnRelease();
													await Coroutine.Sleep(3000);
												}
												if (CollectionManager.Get().GetTaggedDeck(CollectionManager.DeckTag.Editing).GetTotalCardCount() >= DeckRuleset.GetStandardRuleset().GetDeckSize())
												{
													flag = true;
												}
												if (count > 1)
												{
													flag3 = true;
												}
											}
										}
									}
									int num2 = num + 1;
									num = num2;
									if (!flag3 || num >= 2)
									{
										break;
									}
								}
							}
						}
					}
				}
				await Coroutine.Yield();
			}
		}

		private async Task method_3()
		{
			foreach (HSCard allCard in TritonHs.GetAllCards())
			{
				ilog_0.InfoFormat("[{0}] {1}", allCard.Entity_0.m_staticEntityDef.m_cardId, allCard.Entity_0.m_staticEntityDef.GetName());
				TagMap tags = allCard.Entity_0.GetTags();
				for (int i = 0; i < 700; i++)
				{
					if (tags.HasTag(i))
					{
						ilog_0.InfoFormat("\t{0} = {1}", (GAME_TAG)i, tags.GetTag(i));
					}
				}
				ilog_0.InfoFormat("");
			}
		}

		[CompilerGenerated]
		private Task method_4()
		{
			return method_1();
		}
	}
}
